Re: Multi dimensional array
| From: | Ashley Sheridan | Date: | Mon, 03 Aug 2015 05:22:24 +0000 |
| Subject: | Re: Multi dimensional array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-325026@lists.php.net to get a copy of this message | ||
On Sun, 2015-08-02 at 23:19 -0400, Ron Piggott wrote:
> How do I correctly make "category_assignment" into a multi dimensional
> array:
>
> if(is_array($result) &&count($result) >'0') {
> foreach($resultAS$value) {
> $array= [
> 'reference'=>$value['reference'],
> 'introductory_paragraph'=>$value['introductoryParagraph'],
> 'category_assignment'=>array[][$value['category']
> =>$value['categoryLink'] ]
> ];
> }
> }
>
> Ron
>
I would imagine you're after something like this:
if(is_array($result) && count($result) > 0)
{
foreach($result as $value)
{
$array= (
'reference' => $value['reference'],
'introductory_paragraph' => $value['introductoryParagraph'],
'category_assignment' =>
array(
$value['category'] => $value['categoryLink'],
),
);
}
}
I also amended the second clause of the if statement, as you were
comparing an integer to the string 0, and comparing numbers to strings
like that doesn't always work as you want or expect.
The main thing I think you missed was that array constructs use () and
not [] in their creation, which you were using in both of your arrays,
that way is a little more how Javascript does it.
Thanks,
Ash
http://www.ashleysheridan.co.uk