Jump to content

Extracting data from Multi dimensional array


williamZanelli

Recommended Posts

Hello guys

 

I have a serlised array inside my database [i.e. an multi dimensional array that I serlized]

 

when I do echo($array)  it prints the following

 

  a:1:{i:0;O:8:"stdClass":2:{s:18:"category_safe_name";s:9:"Liverpool";s:13:"category_name";s:9:"Liverpool";}} 

 

I then unserialize the array and check the size [using sizeof()] and it prints 1. So far so good.

[The code below is inside a .tpl file]

 

{php}
$array = unserialize($array_field1);	
echo("SizeOf = " . sizeof($array));
			for ($counter=0; $counter < sizeof($array); $counter = $counter +1){
			print_r($array);
			$var_safename = $array[$counter][category_safe_name];
			$var_url = $array[$counter]['category_name'];

			echo("first var ". $var_url);
			echo("second var " . $var_safename);
			{/php}

			, <a href="{$var_url}">{$var_safename}</a>
			{php}
                         }
                                {/php}
			{/if}

 

The $var_url and $var_safename in the echo statments don't print anything.

 

Am I extracting the data from from unserialized array incorrectly?

 

Thanks in advance for your thoughts.

 

Regards

 

Will

Link to comment
Share on other sites

You are using a counter starting at 0 and incrementing by 1 to determine the key. But, from the serialized value it appears the array isn't using 0,1, 2, ... as the key values. You should be using foreach() instead.

 

Also, you are using [category_safe_name] to extract one of the values. That text should be enclosed in quotes unless that is a constant.

 

<?php

foreach ($array as $subarray){
    echo ", <a href=\"{$subarray['category_name']}\">{$subarray['category_safe_name']}</a>";
}

?>

Link to comment
Share on other sites

What is printed to the page when you run this?

<?php

$array = unserialize($array_field1);
print_r($array);

foreach ($array as $subarray){
    echo ", <a href=\"{$subarray['category_name']}\">{$subarray['category_safe_name']}</a>";
}


?>

Link to comment
Share on other sites

print_r doesn't print anything.

 

The for each comes up with following error.

 

Warning: Invalid argument supplied for foreach() in /home/fhlinux147/f/*******.com/user/htdocs/templates_c/c_7e3f5c748c2d5b3d2782c554a847d9e9.php on line 193 

 

The code (posted above) is inside a .tpl (template file).

 

Any ideas?

Link to comment
Share on other sites

Well a tpl file would lead me to believe it is a template. But, PHP doesn't care what a file's extension is. I will "assume" that the template file is being read/processed in a specific manner such that code is not parsed. Without knowing how the tpl file is used there is no way for me to provide any furhter assistance.

Link to comment
Share on other sites

<?php
$ser = 'a:1:{i:0;O:8:"stdClass":2:{s:18:"category_safe_name";s:9:"Liverpool";s:13:"category_name";s:9:"Liverpool";}}';
$subarray = unserialize($ser);

echo "<a href=\"{$subarray[0]->category_name}\">{$subarray[0]->category_safe_name}</a>";
?>

-->
<a href="Liverpool">Liverpool</a>

Link to comment
Share on other sites

Barnard - I'm not sure what your code means... could you please dwell on this?

 

DarkWater - Yes this is a smarty template

 

Yes.. well my thinking is this - I have a 2 dimensional array, I serialize this to store in the database

 

And when I retrieve this from the database its a string, hence in the smarty templete when I write (in the .tpl file) (this is the part that I refer to as echo in my first post)

 

{$array_field1}

 

I get this output

  a:1:{i:0;O:8:"stdClass":2:{s:18:"category_safe_name";s:9:"Liverpool";s:13:"category_name";s:9:"Liverpool";}} 

 

I then unserialize the variable $array_field1 - to convert back into an array so I can have a foreach loop as

 

foreach ($array as $subarray){
    echo ", <a href=\"{$subarray['category_name']}\">{$subarray['category_safe_name']}</a>";
}

 

 

So the code looks something like this

 

			{if $array_field1 neq ""}
			<b>{$array_field1} </b>
			{php}

			$array = unserialize($array_field1);	
				echo("Sizeof = " . sizeof($array));
				print_r($array);
				echo(":: is this an array: " . is_array($array));
			foreach ($array as $subarray){
				{/php}
   , <a href="{$subarray['category_name']}">{$subarray['category_safe_name']}</a></a>

				{php}
				}
				{/php}
				{/if}

 

Hope this helps guys,

 

Thanks for your help so far.

 

Will

Link to comment
Share on other sites

Ahh and please ignore this code - I just put it in to see where I'm going wrong, it wont be in the final version.  ;D

 

echo("Sizeof = " . sizeof($array));
				print_r($array);
				echo(":: is this an array: " . is_array($array));

 

 

So what do you think I'm missing?

 

Have a stored the data incorrectly?  ???

 

Thnaks in advance.

Link to comment
Share on other sites

Barnard - I'm not sure what your code means... could you please dwell on this?

 

 

I thought it pretty self-evident.

 

It unserializes the data and shows you how to access the two items within the array (actually a single object with two variables)

 

$subarray[0]->category_name

and

$subarray[0]->category_safe_name

Link to comment
Share on other sites

DarkWater - The $array_field is set in the PHP,

 

I know this because when I write in the tpl file

 

{$array_field1}

 

It prints out the following

 

 a:1:{i:0;O:8:"stdClass":2:{s:18:"category_safe_name";s:9:"Liverpool";s:13:"category_name";s:9:"Liverpool";}} 

 

Which is a serialised version of a Multi dimensional array.

 

I want to convert this back to a multidimensional array and extract my data (so I unserialize) and try using the foreach loop [as stated above, which casuses some errors]

 

Is my logic correct? Or is there a better way of doing this?

 

Thank you for your help/

 

William

Link to comment
Share on other sites

 

 

Right guys I've been doing some more tests...  ???

 

Here's what I've found

In the tpl file, if I remove this line

 

 $array = unserialize($link_field1);	

 

sizeof($array)

returns 0. If the line stays it returns 1.

 

the error as reported above (and shown below)  appears if the $array is serialized or not

 

Warning: Invalid argument supplied for foreach() in /home/fhlinux147/.... on line 141

 

Is this a template issue? or something to do with my code?

 

Thanks

Will

 

PS: Sorry Barand - I'm new to PHP - wasn't sure what your post was refferring to.

 

Link to comment
Share on other sites

You keep saying that it's a multidimensional array - it isn't. It's an array with a single element, an object.

 

<?php
$ser = 'a:1:{i:0;O:8:"stdClass":2:{s:18:"category_safe_name";s:9:"Liverpool";s:13:"category_name";s:9:"Liverpool";}}';
$array = unserialize($ser);
/**
* view the array
*/
echo '<pre>', print_r($array, true), '</pre>';

?>

 

The result of the above code is

 

Array
(
    [0] => stdClass Object
        (
            [category_safe_name] => Liverpool
            [category_name] => Liverpool
        )

)

Link to comment
Share on other sites

Hey there Barand

 

Thanks for the reply.

 

I tried what you asked for I still get no output -

 

					echo("safe name" . $array[0]->category_safe_name);
				echo("<br/>");
				echo("cat name" . $array[0]->category_name);

 

I think this may have something to do with the fact this code is inside a tpl file.

 

As for multi dimensional - my understanding is that an array that has other arrays inside it is a multi dimensional - the $array here contains results from a database query, namely $category_safe_name and $category name with their corresponding values, my understand was in PHP a database query returns an array - is that so??

 

What I did when i stored the array in the database was create an empty array and then push the result from the database query onto empty array [more results added to the same array later on in the process somwhere].

 

Anyway, thansk for your thoughts so far,

 

Will

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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