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.
Related
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
Most likely I'm doing this wayyyyyy too complicated. But I'm in the need of converting multiple arrays to multidimensional array key's. So arrays like this:
Array //$original
(
[0] => 500034
[1] => 500035 //these values need to become
//consecutive keys, in order of array
)
Needs to become:
Array
(
[50034][50035] => array()
)
This needs to be done recursively, as it might also require that it becomes deeper:
Array
(
[50034][50036][50126] => array() //notice that the numbers
//aren't necessarily consecutive, though they are
//in the order of the original array
)
My current code:
$new_array = array();
foreach($original as $k => $v){ //$original from first code
if((gettype($v) === 'string' || gettype($v) === 'integer')
&& !array_key_exists($v, $original)){ //check so as to not have illigal offset types
$new_array =& $original[array_search($v, $original)];
echo 'In loop: <br />';
var_dump($new_array);
echo '<br />';
}
}
echo "After loop <br />";
var_dump($new_array);
echo "</pre><br />";
Gives me:
In loop:
int(500032)
In loop:
int(500033)
After loop
int(500033)
Using this code $new_array =& $original[array_search($v, $original)]; I expected After loop: $new_array[50034][50035] => array().
What am I doing wrong? Been at this for hours on end now :(
EDIT to answer "why" I'm trying to do this
I'm reconstructing facebook data out of a database. Below is my own personal data that isn't reconstructing properly, which is why I need the above question answered.
[500226] => Array
(
[own_id] =>
[entity] => Work
[name] => Office Products Depot
[500227] => Array
(
[own_id] => 500226
[entity] => Employer
[id] => 635872699779885
)
[id] => 646422765379085
)
[500227] => Array
(
[500228] => Array
(
[own_id] => 500227
[entity] => Position
[id] => 140103209354647
)
[name] => Junior Programmer
)
As you can see, the ID [500227] is a child of [500226], however, because I haven't got the path to the child array, a new array is created. The current parentage only works to the first level.
[own_id] is a key where the value indicates which other key should be its parent. Which is why the first array ([500226]) doesn't have a value for [own_id].
If you want to do something recursively, do it recursively. I hope that's what you meant to do.
public function pivotArray($array, $newArray)
{
$shifted = array_shift($array);
if( $shifted !== null )
{
return $this->pivotArray($array, array($shifted=>$newArray));
}
return $newArray;
}
$array = array(432, 432532, 564364);
$newArray = $this->pivotArray($array, array());
Edit: After the question's edit it doesn't seem to be very relevant. Well, maybe someone will find it useful anyway.
I've never really used PHP arrays before as I've just used a database in the past so I'm broadening my horizons a little. Basically I have a simple nested array where each element has a 'name' and some other values. I'm trying to search through the array, which is part of an object. I've looked through a number of previous questions on here and can't get it working, although in the other cases there haven't been objects involved. I've been trying to use a 'needle/haystack' type example but haven't got it working yet.
So in my People class we have among other things:
public $peopleArray; // this is the array and will be protected once working
// and this is the example search function im trying to modify
public function findPerson($needle, $haystack)
{
foreach($haystack as $key=>$value)
{
if(is_array($value) && array_search($needle, $value) !== false)
{
return $key;
}
}
return 'false';
}
And then to call this I currently have:
$searchResult = $People->findPerson('Bob',$people->peopleArray,'name');
I'm not sure if I'm just confusing myself with what the $needle and $value - I need to pass the name value in the search function, so I did have $value in the function arguments, but this still returned nothing. Also I'm not 100% on whether '$key=>$value' needs modifying as $key is undefined.
Thanks in advance for any help.
Addition - print_r of the array:
Array ( [0] => Person Object ( [id:protected] => 1 [name] => Bob [gender] => m )
[1] => Person Object ( [id:protected] => 2 [name] => Denise [gender] => f )
[2] => Person Object ( [id:protected] => 3 [name] => Madge [gender] => f ) )
Ok this would be a lot easier if you had to post an example array, but I'm going to give this question a shot.
To me it seems you are looping through a 2D array (nested array).
I would loop through multidimensional arrays like such:
for($array as $key => $2ndArray){
for($2ndArray as $2ndKey => $value){
if($value == $needle){
return true;
}
}
}
Hope this helps
I want to check only the value [id] for duplicates, and remove all keys where this "field" [id] is a duplicate.
Example: If I have numbers 1,2,1. I want the result to be 2, not 1,2. And criteria for duplicates is determined only by checking [id], not any other "field".
Original array:
Array
(
[0] => Array
(
[name] => John
[id] => 123
[color] => red
)
[1] => Array
(
[name] => Paul
[id] => 958
[color] => red
)
[2] => Array
(
[name] => Jennifer
[id] => 123
[color] => yellow
)
)
The result I want:
Array
(
[0] => Array
(
[name] => Paul
[id] => 958
[color] => red
)
)
I agree with everyone above, you should give us more information about what you've tried, but I like to code golf, so here's a completely unreadble solution:
$new_array = array_filter($array, function($item) use (&$array){
return count(array_filter($array, function($node) use (&$item){
return $node['id'] == $item['id'];
})) < 2;
});
This should be fairly easy to accomplish with a couple of simple loops:
set_time_limit(0); // Disable time limit to allow enough time to process a large dataset
// $items contains your data
$id_counts = array();
foreach ($items as $item) {
if (array_key_exists($item['id'], $id_counts)) {
$id_counts[$item['id']]++;
} else {
$id_counts[$item['id']] = 1;
}
}
for ($i = count($items); $i >= 0; $i--) {
if ($id_counts[$items[$i]['id']] > 1) {
array_splice($items, $i, 1);
}
}
Result:
Array
(
[0] => Array
(
[name] => Paul
[id] => 958
[color] => red
)
)
While there are neater ways to do it, one advantage of this method is you're only creating new arrays for the list of ids and duplicate ids and the array_splice is removing the duplicates from the original array, so memory usage is kept to a minimum.
Edit: Fixed a bug that meant it sometimes left one behind
This is a very basic approach to the answer and I am sure there are much better answers however I would probably start by doing it the way I would on paper.
I look at the first index, check its value. Then I go through every other index making note of their index if the value is the same as my originally noted value. Once I have gone through the list if I have more than one index with that particular value I remove them all (starting with the highest index, so as to not affect indexes of the others while deleting).
Do this for all other indexes till you reach the end of the list.
It is long winded but will make sure it removes all values which have duplicates. and leaves only those which originally had no duplicates.
function PickUniques(array $items){
// Quick way out
if(empty($items)) return array();
$counters = array();
// Count occurences
foreach($items as $item){
$item['id'] = intval($item['id']);
if(!isset($counters[$item['id']])){
$counters[$item['id']] = 0;
}
$counters[$item['id']]++;
}
// Pop multiples occurence ones
foreach($counters as $id => $occurences){
if($occurences > 1){
unset($counters[$id]);
}
}
// Keep only those that occur once (in $counters)
$valids = array();
foreach($items as $item){
if(!isset($items[$item['id']])) continue;
$valids[$item['id']] = $item;
}
return $valids;
}
Try this one :)
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"
}
}