Looping through array and changing value with if statement - php

I have the following array of data from a DB and would like to change the STATUS value to a string if it matches criteria
Array ( [0] => stdClass Object ( [ORDER] => 1321[DATE] => 2015-10-05 [TEXT] => TESTING [OTIME] => 06:03:03 [STATUS] => 3 [CODE] => ABC) [1] => stdClass Object ([ORDER] => 1321[DATE] => 2015-10-05 [TEXT] => TESTING [OTIME] => 06:03:03 [STATUS] => 3 [CODE] => ABC ) )
I want to loop through the data and do and do something like below:
if (STATUS=3) {
STATUS ="replace the number with some text";
}

Lets say your output is stored in $results array so now you can iterate over loop and check like this:
<?php
for($i=0; $i<count($results);$i++){
if($results[$i]->STATUS == "3"){
$results[$i]->STATUS = "text";
}
}
?>

Related

php, compare arrays and append difference

I have these two arrays:
1:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Type 1
[rate] => 100.00
)
[1] => stdClass Object
(
[id] => 2
[name] => Type 2
[rate] => 75.00
)
[2] => stdClass Object
(
[id] => 3
[name] => Type 3
[rate] => 50.00
)
[3] => stdClass Object
(
[id] => 4
[name] => Type 4
[rate] => 50.00
)
)
2:
Array
(
[0] => stdClass Object
(
[name] => Type 1
[rate] => 125
)
[1] => stdClass Object
(
[name] => Type 2
[rate] => 85
)
[2] => stdClass Object
(
[name] => Type 3
[rate] => 65
)
)
What I need to do is compare the two arrays, and append missing items from 1st array to the 2nd one. This will always be the case first array will have more items than the second one.
I have tried using something like:
$result = array_udiff($array1,$array2,
function ($obj_a, $obj_b) {
return $obj_a->name - $obj_b->name;
}
);
but it just returns an empty array
This?
<?php
$arr1 = array(
(object)array("id"=>1,"name"=>"type 1","rate"=>100.00),
(object)array("id"=>2,"name"=>"type 2","rate"=>75.00),
(object)array("id"=>3,"name"=>"type 3","rate"=>50.00),
(object)array("id"=>4,"name"=>"type 4","rate"=>50.00)
);
$arr2 = array(
(object)array("name"=>"type 1","rate"=>125),
(object)array("name"=>"type 2","rate"=>85),
(object)array("name"=>"type 3","rate"=>65)
);
for($i=0;$i<sizeof($arr1);$i++){
$count=0;
for($j=0;$j<sizeof($arr2);$j++){
if($arr1[$i]->name == $arr2[$j]->name){
$count++;
}
}
if($count==0){
array_push($arr2,(object)array("name"=>$arr1[$i]->name,"rate"=>$arr1[$i]->rate));
}
}
print_r($arr2);
?>
Doesn't need to be complicated, assuming that you allow the arrays to contain objects of the same type and structure. We don't have enough context given the question to understand whether there is a good reason you can't.
//$array1 original array
//$array2 target array
$array2 = array_merge($array1, $array2);

PHP output an Array

i`ve an array with more arrays inside.
But how can i get a specific value from it ?
For example how can i output the first name "John" ?
I tried this but it doesn't work.
foreach($arr as $result) {
echo $result['CLIENTS']['FIRSTNAME'];
}
This is the < pre> output of the array
Array
(
[WHMCSAPI] => Array
(
[ACTION] => getclients
[RESULT] => success
[TOTALRESULTS] => 12
[STARTNUMBER] => 0
[NUMRETURNED] => 12
[CLIENTS] => Array
(
[CLIENT] => Array
(
[ID] => 14
[FIRSTNAME] => John
[LASTNAME] => Doe
[COMPANYNAME] => Muster Company
[EMAIL] => info#mustermann.de
[DATECREATED] => 2014-04-13
[GROUPID] => 0
[STATUS] => Active
)
)
)
)
The other answers & comments are almost right, but they didn't notice that you're iterating over the array.
Here's your code:
foreach($arr as $result) {
echo $result['CLIENTS']['FIRSTNAME'];
}
Inside the loop (this is important), $result is equal to:
Array
(
[ACTION] => getclients
[RESULT] => success
[TOTALRESULTS] => 12
[STARTNUMBER] => 0
[NUMRETURNED] => 12
[CLIENTS] => Array
(
[CLIENT] => Array
(
...
So to access the client name inside the loop, use this:
$firstname = $result["CLIENTS"]["CLIENT"]["FIRSTNAME"];
You're missing a couple levels of the array. Try $result['WHMCSAPI']['CLIENTS']['CLIENT']['FIRSTNAME'].

Get JSON value from PHP

All, I have got a JSON response from NEO4J:
Array
(
[columns] => Array
(
[0] => n
)
[data] => Array
(
[0] => Array
(
[0] => Array
(
[outgoing_relationships] => http://localhost:7474/db/data/node/1/relationships/out
[labels] => http://localhost:7474/db/data/node/1/labels
[data] => Array
(
[position] => Developer
[awesome] => 1
[name] => Michael
[children] => 3
)
[traverse] => http://localhost:7474/db/data/node/1/traverse/{returnType}
[all_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}
[property] => http://localhost:7474/db/data/node/1/properties/{key}
[self] => http://localhost:7474/db/data/node/1
[properties] => http://localhost:7474/db/data/node/1/properties
[outgoing_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}
[incoming_relationships] => http://localhost:7474/db/data/node/1/relationships/in
[extensions] => Array
(
)
[create_relationship] => http://localhost:7474/db/data/node/1/relationships
[paged_traverse] => http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}
[all_relationships] => http://localhost:7474/db/data/node/1/relationships/all
[incoming_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}
)
)
)
)
I do not know how to retrieve the value "position" under the data array, because the array is under another array.
Can you tell me how to do it with PHP?
Thx
Access them like this.
echo $yourarr['data'][0][0]['data']['position'];
Tips on how to locate :
Just locate where is the position keyword, Now look up to the array, As you can see the parent of position is data, think of how you reach from the start
(data)to the destination(position) (like a maze).
When you have more than 1 record try this
foreach($var['data'] as $inside){ //inner 1st stage
foreach($inside as $index => $main){ //inner 2nd stage
if($index == 'data'){ //check if index is data
echo $main['position']; //output position
}
}
}

Arbitrary element in array (the value) to a key that is an array

So, I have an array that looks like this:
$grades =
Array(
[0] => Array ( [Grade] => Array ( [id] => 0 [name] => Kindegarten ) )
[1] => Array ( [Grade] => Array ( [id] => 1 [name] => First ) )
[2] => Array ( [Grade] => Array ( [id] => 2 [name] => Second ) )
[3] => Array ( [Grade] => Array ( [id] => 3 [name] => Third ) )
[4] => Array ( [Grade] => Array ( [id] => 4 [name] => Fourth ) )
[5] => Array ( [Grade] => Array ( [id] => 5 [name] => Fifth ) )
)//End array
I am wondering is there a way to arbitrarily way to pick a key (the first 0-5)? CakePHP returns the items in my table like this and I don't understand how I can conform the to the printing of Form helper with the options.
echo $this->Form->input('grade_selection',
array('type' => 'radio', 'options' => array($grades[?]['Grade']['id'] => $grades[?]['Grade']['name'])));
The ? being how to get it to change in the option so I can get each of the items in the array?
class GradesController extends AppController {
public function index(){
//Gets all the rows in the grade table and stores it into a variable called grade.
//$grades = $this->Grade->find('all');
$grades = $this->Grade->getGrades();
//Returns the $grades variable when it is requested.
if($this->request->is('requested')){
return $grades;
}
//Sends the $grades variable to the view file. The string 'grades' is the name of the variable that the Grades View file will have the same setup as the $grades.
$this->set('grades', $grades);
}
}
I am wondering is there a way to arbitrarily way to pick a key (the first 0-5)?
Yes, php, not CakePHP
$keys = array_keys($grades);
$foo = $keys[rand(0, count($keys) - 1)]
Now, I don't think that's what you need. I see you are using a radio button, what do you want to display in the radio button(s)?
EDIT
Assuming this data come from a Model named "Grade", you would do this in your crontroller
$grades = $this->Grade->find('list');
$this->set(compact('grades'));
Then, in your view:
echo $this->Form->input('grade_selection', array('options' => $grades));

Store into a database table as row the query result

query result
Array
(
[0] => stdClass Object
(
[ingredientID] => 2
[code] => Bf
[description] => 1st Class Flour
[volume] => 8268
[price] => 750
[amount_gram] => 0.02980
[status] => Inactive
[uom_id] => 1
[flour] => Yes
)
[1] => stdClass Object
(
[ingredientID] => 3
[code] => Sf
[description] => 3rd Class Flour
[volume] => 18490
[price] => 635
[amount_gram] => 0.02540
[status] => Inactive
[uom_id] => 5
[flour] => Yes
)
..........
I want to store this results into another table as row inventory.
the table will look like this:
ID inventory
1 (the result)
2 (another result)
And after I will query it back again so that I can display the result.
here's what I have done lately.
store:
//getting the result
$inv = $this->db->get_from('table','id'=>'1')->row();
<input type="hidden" name="inventory" value="<?php print_r($inv)?>">
//storing in the new table
$this->db->insert('table2',array('inventory'=>$this->input->post('inventory')));
getting:
$inventory = $this->db->get_where('table2',array('ID'=>'1'))->row_array();
//result
array
(
[ID] => 1
[inventory] =>
array
(
[0] => stdClass Object
(
[ingredientID] => 2
...... and so on
I want to display everything in the array['inventory'] which is an array of objects.
I've done this
foreach($arr['inventory'] as $invent):
echo $invent['ingredientID'];
but there's an error in the foreach part.
error: Invalid argument supplied for foreach()
What should i do?
endforeach;
assuming:
$results = $this->db->get_where('table2',array('ID'=>'1'))->row_array();
you should use this to print it
foreach($results['inventory'] as $inventory)
{
print_r($inventory->ingredientID);
}

Categories