Jump to content

splitting array in array


davemere

Recommended Posts

Hi there

 

I'm building anl app where I'm passing a number of different variables to my pages. The one I'm interested in right now is an array, which itself contains what I think is another array. I need to split out one particular field so I can change the way it is processed by the front end. Problem is that I've been unable to find the syntax to help me do this!  ???

 

I've found a function that lets me display what the variables looks like:

 

array(1) { [0]=> array(3) { ["value"]=> string(14) "string content" } }

 

I need to extract the string "string content". I think what I'm looking at here is an array of an array. Could anyone suggest what code I can use to split out this string?

 

I'm very new to php, so please excuse this basic question!

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/128111-splitting-array-in-array/
Share on other sites

It's just a multidimensional array.  In your case:

 

<?php
$array1=array('array2'=>array('content'=>'string content'));
$string=$array1['array2']['content'];
echo $string;  //Will out put 'string content';

 

Also, to view the structure of an array just use print_r($array);

Just to let you know, you can use print_r() like this to make it even more readable:

 

<?php
$array = array('something' => array('complex', 'really complex', 'complex' => 'times 2'));
echo '<pre>' . print_r($array, true) . '</pre>';
?>

 

Using it in <pre> tags preserves the spacing.

PHP CLI Also preserves the spacing without <pre> tags, which is why it's not there by default ;)

 

Only because it's used on the command line and \n actually means something to the shell (means nothing to the web browser, just formats source code...).  I use the CLI all the time (hell, I think I use it more than the CGI sometimes) to test scripts and little pieces of code.  I open up like emacs and write some code, save it to a temp. file (just easier) and run it in the CLI.

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.