Jump to content

Multidimensional arrays


themistral

Recommended Posts

Hi guys,

 

I have a multidimensional array stored as a string in a database.

How can I retrieve this information and make it act as a string?

 

$query = mysql_query("SELECT string FROM database");
$result = mysql_fetch_array($query);

$arr = array();
$arr[] = $result[string];

 

Would this have the required effect to make a string act as a multidimensional array?

Link to comment
Share on other sites

This is an example of the string in the database

a:1:{s:7:"options";a:3:{s:11:"Yellow/Pink";s:11:"Yellow/Pink";s:12:"Yellow/Green";s:12:"Yellow/Green";s:12:"Yellow/Multi";s:12:"Yellow/Multi";}}

 

I need to access the values of "Yellow/Green", "Yellow/Pink" and "Yellow/Multi" to populate a drop down list.

 

:D

Link to comment
Share on other sites

OK, I've got my multidimensional array:

 

Array
(
    [options] => Array
        (
            [Animal Print] => Animal Print
            [Animal Print/Multi] => Animal Print/Multi
            [Animal Print/Pink] => Animal Print/Pink
            [black] => Black
        )

)

 

I can print my array fine.

$data = unserialize($result['colours']);
echo '<pre>';
print_r($data);
echo '</pre>';

I can reference a single item using

echo $data['options']['Animal Print'];

But I can't seem to reference a single item using

echo $data[0][0];

 

Can anyone help me out?

Link to comment
Share on other sites

themistral, you could use a function like the one below to turn the associative nested array into a plain nested array.

 

function unassoc(&$array) {
// Re-index the array
$array = array_values($array);
// Call unassoc recursively for sub-arrays
foreach ($array as &$val) {
	if (is_array($val)) {
		unassoc($val);
	}
}
}

// Use it like this
unassoc($data);

echo $data[0][0];

 

Link to comment
Share on other sites

I've got to ask why you want a numerically indexed array? I suspect is it because you want to iterate over the data, in which case you would just iterate over the associative array.

 

Given that the number of data elements could vary, the meaning of each element looks like it can be anything, and even the order of the elements could change, attempting to access a specific numerical element wouldn't necessarily give you the result you expect.

 

 

Link to comment
Share on other sites

I need to loop through the items to create a dropdown box.

 

I have done it now - phew!

 

	$data = unserialize($result['colour']);

$data2 = $data['options']; // options is the name of the first array

foreach ($data2 as $value) {
	echo '<option value="'.$value.'">'.$value.'</option>';
}

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.