Dorky Posted November 1, 2009 Share Posted November 1, 2009 hey, trying to get a 2d array down to top level as ending with /n and lower level divided by || like so in the flat file linkname || linkurl.com linkname || linkurl.com linkname || linkurl.com linkname || linkurl.com Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 Warning: implode() [function.implode]: Invalid arguments passed in $rewritearray = implode("||" , $dumplinkdata[$dumplinkk]); Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 1, 2009 Share Posted November 1, 2009 // does this show that $dumplinkdata[$dumplinkk] is an array? var_dump($dumplinkdata[$dumplinkk]); Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 Array ( [1] => Array ( [0] => jknjk [1] => jn nj,n.com ) [2] => Array ( [0] => kjnkj [1] => ljknln ) ) Array ( [2] => Array ( [0] => kjnkj [1] => ljknln ) ) Array ( ) Quote Link to comment Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 You want to use explode(), not implode(). implode() takes an array and makes a string, the opposite of what you want to do. <?php $file = <<<FILE linkname || linkurl.com linkname || linkurl.com linkname || linkurl.com linkname || linkurl.com FILE; $arr = array_map(create_function('$a', 'return array_map("trim", explode("||", $a));'), explode("\n", $file)); echo "<pre>"; print_r($arr); echo "</pre>"; Output: Array ( [0] => Array ( [0] => linkname [1] => linkurl.com ) [1] => Array ( [0] => linkname [1] => linkurl.com ) [2] => Array ( [0] => linkname [1] => linkurl.com ) [3] => Array ( [0] => linkname [1] => linkurl.com ) ) Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 no. i need to make array([0] array( [0] , [1] )) array([1] array( [0] , [1] )) array([2] array( [0] , [1] )) array([3] array( [0] , [1] )) into [0] || [1] [0] || [1] [0] || [1] [0] || [1] a string to put in a flat file. linkname || linkurl linkname || linkurl linkname || linkurl linkname || linkurl with an unknown number of parent arrays Quote Link to comment Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 So like the opposite of what I did? I don't really follow what you're saying.. Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 no. i need to make array([0] array([0] , [1] )) array([1] array([0] , [1] )) array([2] array([0] , [1] )) array([3] array( [0] , [1] )) into [0] || [1] [0] || [1] [0] || [1] [0] || [1] a string to put in a flat file. linkname || linkurl linkname || linkurl linkname || linkurl linkname || linkurl with an unknown number of parent arrays Quote Link to comment Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 <?php $arr = Array( Array('linkname', 'linkurl.com'), Array('linkname', 'linkurl.com'), Array('linkname', 'linkurl.com') ); $var = implode("\n", array_map(create_function('$a', 'return implode(" || ", $a);'), $arr)); Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 1, 2009 Share Posted November 1, 2009 $arr = array( 0 => array('linkname1' => 'linkurl1.com'), 1 => array('linkname2' => 'linkurl2.com'), 2 => array('linkname3' => 'linkurl3.com') ); $str = ''; foreach($arr as $v): foreach($v as $key => $val) $str .= $key . ' || ' . $val . "\n\r"; endforeach; echo nl2br($str); //EDITED, should display correct result now. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 1, 2009 Share Posted November 1, 2009 <!-- #Output --> linkname1 || linkurl1.com linkname2 || linkurl2.com linkname3 || linkurl3.com Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 i see that would work but like i said the number of arrays will very. so i cant just simply reassign the arrays as is, it must be dynamic. im still looking around as well on google. nothing yet. funny, i would think this is commons. Quote Link to comment Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 i see that would work but like i said the number of arrays will very. so i cant just simply reassign the arrays as is, it must be dynamic. im still looking around as well on google. nothing yet. funny, i would think this is commons. I don't know what you're talking about.. My solution will work with any number of arrays.. Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 $arr = array( 0 => array('linkname1' => 'linkurl1.com'), 1 => array('linkname2' => 'linkurl2.com'), 2 => array('linkname3' => 'linkurl3.com') ); is a determined number of arrays, i dont see how that applies to an unlimited number of them. Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 if ($_GET['dump'] == "link" && $_SESSION['staff'] == "on") { $dumplink = $_GET['access']; $dumplinkinfo = file("$dumplink.html"); foreach($dumplinkinfo as $dumplinkkey => $dumplinkval) { $dumplinkdata[$dumplinkkey] = explode("||", $dumplinkval); } for($dumplinkk = 0; $dumplinkk < sizeof($dumplinkinfo); $dumplinkk++) { unset($dumplinkdata[$dumplinkk][0]); unset($dumplinkdata[$dumplinkk][1]); unset($dumplinkdata[$dumplinkk]); } $test = sort ($dumplinkdata); print_r($test); } i need to get out of this the same thing i get with print_r in the form of $dumplinkdata[$dumplinkk][0] || $dumplinkdata[$dumplinkk][1] final write to file should look like This is a link || http://studio378d.com Quote Link to comment Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 That's just a static array for the example.. You can plug any other Array with any other number of elements and it'll work. Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 lmao. ok im not understanding. im really tired and near the end of this project. im not trying to p!$$ you off at all. if you could give me the tard breakdown i would greatly appreciate it. Quote Link to comment Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 $var = implode("\n", array_map(create_function('$a', 'return implode(" || ", $a);'), $arr)); First array_map() is preformed on $arr which is our multidimensional array. For every array it preforms implode(" || ", $a), which basically takes each array and implodes with a delimiter of " || ". So basically array_map() in this case overall returns an array of strings like "linkname || linkurl.com", finally the last implode() takes that array returned by array_map() and creates a string with a delimiter of "\n". array_map() create_function() implode() Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 ok i got that. very cool man thx very very much. i am on coffee overload. this is what i have and its doing what it needs to but it either isnt unseting the file or its adding link breaks. not sure yet but i have a line for each time i read , remove and replace. if ($_GET['dump'] == "link" && $_SESSION['staff'] == "on") { $dumpthis = ereg_replace("[^A-Za-z0-9]", "", $_GET['dumplink']); $dumplinkinfo = file("links.html"); foreach($dumplinkinfo as $dumplinkkey => $dumplinkval) { $dumplinkdata[$dumplinkkey] = explode("||", $dumplinkval); } for($dumplinkk = 0; $dumplinkk < sizeof($dumplinkinfo); $dumplinkk++) { $check = ereg_replace("[^A-Za-z0-9]", "", $dumplinkdata[$dumplinkk][0]); if ($check == $dumpthis) { unset($dumplinkdata[$dumplinkk][0]); unset($dumplinkdata[$dumplinkk][1]); unset($dumplinkdata[$dumplinkk]); } } $linkvar = implode("\n", array_map(create_function('$a', 'return implode(" || ", $a);'), $dumplinkdata)); unlink("/home/p14tnue3/public_html/howton/links.html"); $fp = fopen("links.html", "a"); fwrite($fp, "$linkvar"); fclose($fp); } Quote Link to comment Share on other sites More sharing options...
Dorky Posted November 1, 2009 Author Share Posted November 1, 2009 hmm. idk. its just fine. i cleared all the white space and uploaded then deleted the links and its perfect. you are the man. thx for being patient. Quote Link to comment Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 Np, just remember to mark the topic as solved, there's a button at the bottom left to do this. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.