Jump to content

[SOLVED] easy - get data out of an array


teknojunkey

Recommended Posts

Hi , im trying to get the data out of an array.

 

I can see the array using this:

print_r($_SESSION); 

 

this is printed:

Array ( [submitted] => Array ( [1] => Array ( [code] => Array ( [4mmPilkingtonMinisterToughened] => TRUE ) ) [3] => Array ( [code] => Array ( [RecetrackCutimgsrcimagesracetrack.jpg] => TRUE ) ) [4] => Array ( [other] => 10 ) [5] => Array ( [other] => 200mm ) [6] => Array ( [other] => 200mm ) ) ) 

 

I want to get the following out of the array and displayed without the "other bits":

 

"4mmPilkingtonMinisterToughened" "RecetrackCutimgsrcimagesracetrack.jpg" "10" "200mm" "200mm"

 

Im a bit of a newby so please be gentle.

 

thanks :)

 

Link to comment
https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/
Share on other sites

You need teh foreach loop.

 

foreach($_SESSION['submitted'] as $key => $value) {
$data[]['key'] = $key;
$data[]['value'] = $value;
} 
echo $data[0]['key'] . "\n";
echo $data[1]['key'] . "\n";
echo $data[2]['value'] . "\n";
echo $data[3]['value'] . "\n";
echo $data[4]['value'] . "\n";

 

we are putting the array keys and values in an array and selectively fishing them out.

 

Martin

Thanks I got the first item in teh array out , but nothing els.

 

I used this :

 

foreach($_SESSION['submitted'][1]['code'] as $nkey => $nvalue) {

$data[]['nkey'] = $nkey;

$data[]['nvalue'] = $nvalue;

}

print_r ($data[0]['nkey'] . "\n");

print_r ($data[1]['nvalue'] . "\n");

print_r ($data[2]['nkey'] . "\n");

print_r ($data[3]['nvalue'] . "\n");

 

can i replace the [1] with a wildcard ? if there is one

thanks

 

 

i have figured out what i want to do ,

 

I want this to display the new data in the array as it is added.

 

foreach($_SESSION['submitted'][1]['code'] as $nkey => $nvalue) {

$data[]['nkey'] = $nkey;

$data[]['nvalue'] = $nvalue;

}

print_r ($data[0]['nkey'] . "\n");

print_r ($data[1]['nvalue'] . "\n");

print_r ($data[2]['nkey'] . "\n");

print_r ($data[3]['nvalue'] . "\n");

 

currently it only displays the first item in the array, is it possible to update the output text at the array is updated?

 

thanks

 

Hi

is it possible to update the output text as the array is updated?

 

foreach($_SESSION['submitted'][1]['code'] as $nkey => $nvalue) {

$data[]['nkey'] = $nkey;

$data[]['nvalue'] = $nvalue;

}

print_r ($data[0]['nkey'] . "\n");

print_r ($data[1]['nvalue'] . "\n");

print_r ($data[2]['nkey'] . "\n");

print_r ($data[3]['nvalue'] . "\n");

 

the number 1 should change to show the other keys in the array

foreach($_SESSION['submitted'][1]['code'] as $nkey => $nvalue) {
$data[]['nkey'] = $nkey;
$data[]['nvalue'] = $nvalue;
}

$cnt = count($data);
for ($i=0; $i<$cnt; $i++) {
print_r ($data[$i]['nkey'] . "\n");
print_r ($data[$i]['nvalue'] . "\n");
}

 

 

I have to modify and add this for each item in the array :

 

foreach($_SESSION['submitted'][1]['code'] as $nkey1 => $nvalue1) {

$data1[]['nkey1'] = $nkey1;

$data1[]['nvalue1'] = $nvalue1;

}

$cont1 = count($data1);

for ($i=0; $i<$cont1; $i++) {

print_r ($data1[$i]['nkey1'] . "\n");

print_r ($data1[$i]['nvalue1'] . "\n");

}

echo "<br>";

//------------------------------------------------------------

 

foreach($_SESSION['submitted'][3]['code'] as $nkey3 => $nvalue3) {

$data3[]['nkey3'] = $nkey3;

$data3[]['nvalue3'] = $nvalue3;

}

$cont3 = count($data3);

for ($i=0; $i<$cont3; $i++) {

print_r ($data3[$i]['nkey3'] . "\n");

print_r ($data3[$i]['nvalue3'] . "\n");

}

 

No great, but it now works thanks for your help:)

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.