i have an array like this
[multiple_name] => Array
(
[0] => Change the business unit\'s root business unit
[1] => Change the business unit\'s parent business unit
[2] => Copy the business unit to the new position in the organizational hierarchy.
[3] => Disable the business unit, change the business unit s organization team and then activate the business unit
)
[multiple_correct] => Array
(
[1] => yes
)
now i want to print only key/index values from this array like '0,1,2,3' and from next one '1'.
kindly help me through this.. thanx in advance !!
Use foreach and bind the keys to a variable:
foreach($someArray as $key => $value) {
echo $key;
}
Where I supposed (from your question) that multiple_name and multiple_correct are not nested arrays, if they are though you need to foreach twice:
foreach($containerArray as $innerArray) {
foreach($innerArray as $key => $value) {
echo $key;
}
}
Related
I'm quite new with PHP and I'm facing a problem with arrays.
say I have a multidimensional associative array called $charsarray like this:
[1] => ([name] => mickey [surname] => mouse)
[2] => ([name] => donald [surname] => duck)
...
[N] => (...)
I need to extract the "surname" field of each entry so my code has nested foreach:
foreach($charsarray as $key => $value )
{
foreach($value => $singlechar)
{
echo $singlechar
}
}
This outputs both mickey mouse donald duck since these are the values of the associative array.
If I want to extract only surnames I could write an if statement to check against the key surname.
Is there a better approach to this without using the if statement ?
You don't need to loop through the entire thing. You can just reference the specific value in the array by using correct index (surname).
foreach($charsarray as $key => $value )
{
echo $value['surname']
}
Surname also a key in that array so you need to print like as below
foreach($charsarray as $key => $val){
echo $val['surname'];
}
As a newbie, does anyone have any good tutorials to help me understand different levels of an array? What I'm trying to learn is how to echo different levels, e.g. here is the output array:
Array
(
[meta] =>
Array
(
[total_record_count] => 1
[total_pages] => 1
[current_page] => 1
[per_page] => 1
)
[companies] =>
Array
(
[0] =>
Array
(
[id] => 291869
[url] => https://api.mattermark.com/companies/291869
[company_name] => gohenry.co.uk
[domain] => gohenry.co.uk
)
)
[total_companies] => 1
[page] => 1
[per_page] => 1
)
And here is the code to parse the array:
foreach($jsonObj as $item)
{
echo $item['total_companies'];
}
I'm really struggling to find the structure and how to output each items, e.g. tried things like:
echo $item[0]['total_companies'];
echo $item['companies'][0]['id'];
Any help or pointers would be greatly received.
Well, Lets start, You have a multi-dimensional array. For a multi-dimensional array you need to use looping e.g: for, while, foreach. For your purpose it is foreach.
Start with the array dimension, Array can be multi-dimension, Like you have multi-dimension. If you have an array like below, then it is single dimension.
array(
key => value,
key2 => value2,
key3 => value3,
...
)
Now, How can you know what is a multi-dimension array, If you array has another array as child then it is called multi-dimensional array, like below.
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
Its time to work with your array. Suppose you want to access the value of company_name, what should you do?? Let your array name is $arr.
First you need to use a foreach loop like:
foreach($arr as $key => $val)
The keys are (meta, companies, total_companies...), they are in the first dimension. Now check if the key is company_name or not, if it matches than you got it. Or else you need to make another loop if the $val is an array, You can check it using is_array.
By the same processing at the last element your loop executes and find your value.
Learning
Always a good idea to start with the docs:
arrays: http://php.net/manual/en/language.types.array.php
foreach: http://php.net/manual/en/control-structures.foreach.php
As for tutorials, try the interactive tutorial over at codecademy: https://www.codecademy.com/learn/php
Unit 4 has a tutorial on arrays
Unit 11 has a lesson on advanced arrays.
Your code
As for your code, look at the following which I will show you your array structure and how to access each element. Perhaps that will make things clearer for you.
So lets say your array is named $myArray, see how to access each part via the comments. Keep in mind this is not php code, I'm just showing you how to access the array's different elements.
$myArray = Array
(
// $myArray['meta']
[meta] => Array (
// $myArray['meta']['total_record_count']
[total_record_count] => 1
// $myArray['meta']['total_pages']
[total_pages] => 1
// $myArray['meta']['current_page']
[current_page] => 1
// $myArray['meta']['per_page']
[per_page] => 1
)
// $myArray['companies']
[companies] => Array (
// $myArray['companies'][0]
[0] => Array (
// $myArray['companies'][0]['id']
[id] => 291869
// $myArray['companies'][0]['url']
[url] => https://api.mattermark.com/companies/291869
// $myArray['companies'][0]['company_name']
[company_name] => gohenry.co.uk
// $myArray['companies'][0]['domain']
[domain] => gohenry.co.uk
)
)
// $myArray['total_companies']
[total_companies] => 1
// $myArray['page']
[page] => 1
// $myArray['per_page']
[per_page] => 1
)
As for your for each loop
foreach($jsonObj as $item)
{
echo $item['total_companies'];
}
What the foreach loop is doing is looping through each first level of the array $jsonObj, so that would include:
meta
companies
total_companies
page
per_page
Then within the curly braces {} of the foreach loop you can refer to each level by the variable $item.
So depending on what you want to achieve you need to perhaps change your code, what is it you're trying to do as it's not really clear to me.
As for the code within the loop:
echo $item['total_companies'];
It won't work because you're trying to access an array with the index of total_companies within the first level of the $jsonObj array which doesn't exist. For it to work your array would have to look like this:
$jsonObj = array (
'0' => array ( // this is what is reference to as $item
'total_companies' => 'some value'
)
)
What you want to do is this:
foreach($jsonObj as $item)
{
echo $jsonObj['total_companies'];
}
As for your final snippet of code:
echo $item[0]['total_companies'];
Answered this above. Access it like $jsonObj['total_companies'];
echo $item['companies'][0]['id'];
If you want to loop through the companies try this:
foreach($jsonObj['companies'] as $item)
{
// now item will represent each iterable element in $jsonObj['companies]
// so we could do this:
echo $item['id'];
}
I hope that all helps! If you don't understand please make a comment and I'll update my answer.
Please take a look in to here and here
Information about php arrays
Try recursive array printing using this function:
function recursive($array){
foreach($array as $key => $value){
//If $value is an array.
if(is_array($value)){
//We need to loop through it.
recursive($value);
} else{
//It is not an array, so print it out.
echo $value, '<br>';
}
}
}
if you know how deep your array structure you can perform nested foreach loop and before every loop you have to check is_array($array_variable), like :
foreach($parent as $son)
{
if(is_array($son))
{
foreach($son as $grandson)
{
if(is_array($son))
{
foreach($grandson as $grandgrandson)
{
.....
......
.......
}
else
echo $grandson;
}
else
echo $parent;
}
else
echo $son;
}
hope it will help you to understand
I am trying to remove records from a multidimensional array if certain criteria are met. The condition is that the element 'cap' should only have 'Capped' or Uncapped', if 'Other' is there, it should be removed from the array.
I have tried using Unset() but with no luck. It does not break the code, but it changes nothing. Output of the array:
[0] => Array
(
[name] => Club Name
[speed] => Annual Membership
[cap] => Other
[s_description] => Short description
[l_description] => Long description
)
[1] => Array
(
[name] => 50\5Mbps Fibre with 250GB
[speed] => Residential 50/5 Capped
[cap] => Capped
[s_description] => Short description
[l_description] => Long description
)
[2] => Array
(
[name] => FB-FP-B-V-50/5MBPS-400-24
[speed] => Residential 50/5 Capped
[cap] => Capped
[s_description] => Short description
[l_description] => Long description
)
Code for removing the 'Other' record:
foreach ($product_details['cap'] as $key ->$val_test) {
if ($val_test == "Other") {
unset($product_details[$key]);
}
}
I realise I may be doing something really stupid, but for something that should be fairly simple, it seems really difficult!
Thanks
Assuming your main array is stored under the variable: $product_details
Your code would be
foreach($product_details as $key => $product){
if($product['cap'] == "Other"){
unset($product_details[$key]);
}
}
You cant loop over '$product_details['cap']' as that is part of your inner array.
You must loop over your outer array then check the value of the inner array.
I hope this makes sense.
EDIT: On rereading of your question you could change your if statement to this to make it more open ended;
if($product['cap'] != "Capped" && $product['cap'] != "Uncapped"){
My solution
foreach ($your_array as $element) {
if ($element['cap'] == "Other") {
continue;
} else {
$newarray[] = $element;
}
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
Else you have to loop like above. There are other methods to map as well.
supposing I have an array like the one below:
Array
(
[0] => Array
(
[id] => 1
[title] => Group1
[description] => This is the group1.
)
[1] => Array
(
[id] => 2
[title] => Group2
[description] => This is group2.
)
)
Supposing the title is known as "Group2". How would I able to determine using PHP its equivalent description (that is "This is group2") if it doesn't have any idea of its ,key,id, etc. only the title?
Thanks for any help.
Try this :
$title = "Group2";
foreach($your_array as $val){
if($val['title'] == $title){
echo $val['description'];
break; //cut back on unnecessary looping
}
}
Try like this
foreach($myarray as $val){
if($val['title'] == "Group2"){
echo 'This is description '.$val['description'];
}
}
You'll have to iterate over the main array and scan it for that title.
Assuming your main array is called $groups :
$title = 'Group2';
foreach($groups as $key => $group){
if ($group['title'] == $title){
$groupDescription = $group['description'];
// if you need to reference this group again, save it's key.
$groupKey = $key;
}
}
You can insert a break command after you have found the group you are looking for to terminate the loop so that it will not continue to scan the array after you have found the one you are looking for.
Well I'm relatively new to the use of PHP arrays and I have the follow array I need to loop through this array 2280 and 2307 are the identifiers.
I'm find it hard trying to come up with a foreach() that collects all the data.
Array
(
[2280] => Array
(
[0] => http://deals.com.au//uploads/deal_image/2706.jpg
[1] => Yuan's Massage and Beauty
[2] => Get Hair Free in CBD! Only $99 for SIX MONTHS worth of the latest in IPL Permanent Hair Reduction. Choose which area you want treated! Valued at $900 from Yuan's Massage and Beauty in the Heart of Melbourne's CBD. Limited vouchers available
[3] => 99
[4] => 900
[5] => 801
[6] => http://deals.com.au/1827
)
[2307] => Array
(
[0] => http://deals.com.au//uploads/deal_image/2683.jpg
[1] => Name Necklace Australia
[2] => Style yourself like SJP! Only $29 for a STERLING Silver Name Necklace plus get FREE delivery! Valued at $75 with NameNecklace.com.au
[3] => 29
[4] => 75
[5] => 46
[6] => http://deals.com.au/Melbourne
)
)
Your array snippet
$array = array( // foreach ($array as $k=>$subarray)
'2280' /* this is your $k */ =>
array( /* and this is your $subarray */
'http://deals.com.au//uploads/deal_image/2706.jpg',
And now you get data you need (you had to use nested foreach since values of your array are arrays):
foreach ($array as $k=>$subarray) {
foreach ($subarray as $data) {
echo $data;//of subarray
}
}
UPDATE
The OPs comment quistion on my answer:
what if I wanted to be selective rather than get a massive data dump.
if I echo $data how do I access specific rows?
Well, in most cases you should associate keys and data in your array (we call it associative array in PHP).
Example 1:
$hex_colors = array('FF0000', '00FF00' '0000FF');
Values are not assoiated with correspondent keys. PHP will assign 0,1,2... keys to arrays elements itself. In this case you would get green color's heximal value using auto-assigned by PHP 1 key: echo $hex_colors[1]; // 00FF00 and of course you should know it for sure. Usually this approach is used when you have strict data structure like array(red, green, bluee), but in most cases you better use the following approach:
Example 2:
$hex_colors = array('red'=>'FF0000', 'green'=>'00FF00' 'blue'=>'0000FF');
heximal colors representations are associated with appropriate keys echo $hex_colors['green']; // 00FF00
If your array was:
$array = array(
'2280' => array(
'image_url' => 'http://deals.com.au//uploads/deal_image/2706.jpg',
'product_name' => 'Yuan\'s Massage and Beauty',
'description' => 'Get Hair Free in CBD! Only $99 for SIX MONTHS worth of the latest in IPL Permanent Hair Reduction. Choose which area you want treated! Valued at $900 from Yuan's Massage and Beauty in the Heart of Melbourne's CBD. Limited vouchers available',
'price' => 99,
'weight_brutto' => 900,
'weight_netto' => 801,
'dealer_store' => 'http://deals.com.au/1827',
...
You would be able to access data using, let's call it "human-readable" keys:
foreach ($array as $id=>$product) {
echo 'Buy '.$product['name'].'';
echo '<img class="product_thumbnail" src="'.$product['image_url'].'" />';
echo 'Description: '.$product['description'];
echo 'The price is '.number_format($product['price']);
...
}
It is well-suited for reading rows from database, for example:
while ($data = mysql_fetch_array($query_result_link, MYSQL_ASSOC)) {
// MYSQL_ASSOC flag is not necessary
// PHP will use this flag by default for mysql_fetch_array function
// keys of $data will be correspondent columns names returned by mysql query
echo $data['id'];
echo $data['title'];
...
}
Continue learning PHP and you will find many more situations when it's more convenient to associate keys and values.
foreach ($arr1 as $key1 => $value1) {
//$key1==2280; $value1 is the array
foreach ($value1 as $key2 => $value2) {
//$key2==0;$value2="http://deals.com.au//uploads/deal_image/2706.jpg"
}
}