Jump to content

[SOLVED] multidemsional array


Dorky

Recommended Posts

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
        )

)

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

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

$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.

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..

$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.

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

$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()

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);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.