arrays. getting the 3rd item of this multi array - php

Please help. I want to print the price. I need to print the price only. Help me. Thanks
$x=array(array('itm_name'=>'Optimization','price'=>'100','desc'=>'Chuchu'));

It seems you are using a 2 dimensional array
have you tried
echo $x[0]['price']
note '0' is the first element of the 1st layer of the array
so this can be substituted with 'x' so can iterate through a list
additionally if you want to get the other items of the array then
echo $x[0]['itm_name']
i hope you have grasped the basic concept of multi-dimensional arrays
using this simple explanation

Use this:
$x[0]['price']
$x[0] means the first element of the outer array, and ['price'] gets the price element of the inner array.

Related

Push or Merge data into existing Array

When working with existing code, it takes one array and places it into another in the fashion shown below.
I believe the empty brackets are the same thing as simply pushing it and appending it to the first available index.
$g['DATA'][] = $p;
After this is done, I have my own array that I would like to append to this as well. I tried using array_merge() with $g['DATA'][]as a parameter, but this is invalid for obvious reasons.
My only thought is to create a foreach loop counter so I can figure out the actual index it created, however I have to assume there is some cleaner way to do this?
Just simply use the count() of your $g["DATA"] array as index and then you can merge it like this:
$g['DATA'][count($g["DATA"])-1] = array_merge($g['DATA'][count($g["DATA"])-1], $ownArray);
//^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
// -1 Because an array is based index 0 -> means count() - 1 = last key

Comparing like elements in arrays and echoing out rest of the arrays

I have created multiple arrays from text documents that are uploaded monthly. One of the Arrays item numbers, description and picture. the other array is item numbers, price and quantity.
What I am trying to do is if the item number is the same then be able to Echo out all the information that I need. Example would be
if($array1[0] ==$ array2[4]){
echo $array1{4];
echo $array2[6];
}
this doesn't work and having issues with getting array_intersect to work with it also.
I don't really understand how there are so many variable inside an array. Item's number, description, and picture inside an array? I assuming you are looking for matching item's number inside both arrays so..
Good luck trying:
foreach($array1 as $a){
foreach($array2 as $b){
if($a==$b){
echo $a;
}
}
}
Let me know how it works for you soon.
this didnt work for what I was trying to do but I did figure it out. I needed to use strpos() in a if statement to get the items I was looking for.

Count values left in Array

I need to figure out how to get the remaining values left in an Array.
Example: I have an array that randomly get two values. I want to get how many are left in that array after removing it.
If i remove 5 values from the array, and have a total of 20 return value 15.
Edit: If i have 52 cards in a deck, I draw 2 cards I have 50. If I draw two more cards i have 48.
I need to track how many "cards" are in the dealers array.
This should work:
$deck = array(1,2,3,4,5,6,7,8,9,10);
unset($deck[0]);
unset($deck[1]);
echo count($deck);
unset removes the elements from the array count should be 8 afterwards.
See it work: https://eval.in/163527
Edit:
...and remember arrays are 0 based so your first key unless otherwise defined will be 0. But the count() function will tell you the actual number of elements in the array. So $deck[count($deck)]; would be undefined.
This will echo size of array yourarray
<?php
echo(sizeof($yourarray));
?>
Store your array size using a count method such as count or sizeof store it in a variable and perform logical checks on it? i.e if().. etc . Your question is a tad bit broad.
I would recommend that you make use of the PHP count function.
echo count($yourArray);
Should do the trick.

Problems with nested array after using unset

I have a dynamically built nested array from which i need to drop one or more indexes.
Having scoured the php information pages, i found that using
unset($quotes_array[0]['methods'][3]);
would remove the last set of data in the array.
But, if i try to use
unset($quotes_array[0]['methods'][0]);
or any other set apart from the last, it messes up the output that is generated from the array.
For instance, if i had a,b,c,d: I can remove d without issue, but if i try to remove a,
i get a blank radio button followed by b,c, with d missing all together when the array data is processed.
I am assuming i need to reindex the array, but every attempt i've made so far has failed to give the required results, most likely because i'm reindexing $quotes_array, whereas the data i actually need to reindex is that within the 'methods' indices.
Is there a way to fix this issue?
unsetting the first element is easy if you use array_shift. array_shift will pop the first element off and reindex your array for you.
array_shift($quotes_array[0]['methods']);
if you need to unset something in the middle (such as [ 2 ] out of [ 3 ]) you could use array_values. First unset the element you want to remove, then reindex them with array_values.
unset($quotes_array[0]['methods'][2]);
array_values($quotes_array[0]['methods']);
if you wanted to remove the last element of an array you could use array_pop. array_pop will pop the last element off the array. There is no need to reindex in this situation.
array_pop($quotes_array[0]['methods']);
try
array_values($quotes_array[0]['methods']);

How to use these array in php

Can someone explain what is happening in these codes?
I don't understand how cols and rows are being used.
Thank you
$chartdata['cols'][] = array('label'=>'Days Range','type'=>'string');
$chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$prevyearbal)));
how these associative arrays get saved in the two dimensional array?
Is $chardata['cols'][] going from row 0 to row n?
As explained in the documentation, assigning to [] is basically the same as array_push but without the function call.

Categories