Laravel 5.4: how to iterate through request array? - php

My request data represents an array of new and existing items. I'm trying to through this array to update and create items.
This is how I retrieve the array:
$userInput = $request->all();
foreach( $userInput['items'] as $key=>&$item){
Later in the code I update an existing item:
$updateItem = Item::find($item['id']);
$updateItem->number = $item['number'];
$updateItem->save();
But $item['number'] seems to contain old input from previous updates and not the value I entered last time.
How can I loop through request data in Laravel ?
This is the whole code as I run it (want to get rid of confusion):
$userInput = $request->all();
// checking $userInput here
// I can see the new value in the array
foreach( $userInput['items'] as $key=>$item){
if($item['delete'] == 1) {
Item::where('order_id',$order->id)
->where('id',$item['id'])
->delete();
} else {
if(empty($item['id'])) {
} else {
$updateItem = Item::find($item['id']);
$updateItem->number = $item['id'];
$updateItem->save();
}
}
}
This is an input from html (just to show I checked the form also, the data comes just fine):
<input id="basicItemNumber-31" class="form-control" name="items[31][number]" placeholder="Unique number" value="31" type="text">

It's likely that somewhere inside your for you've inadvertently changed the value of your underling $item as you pass it by reference (using the & before the variable name.)
It's considered bad practice by some or most people as it can lead to "unexpected" behaviour, For example. take the sample code below that loops through an array of $items twice once by reference and once by value.
<?php
$items = ['one','two','three'];
foreach ($items as &$item) {
//do nothing.
}
foreach ($items as $item) {
//do nothing again.
}
var_dump($items);
//outputs
array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
&string(3) "two"
}

Try something like this as it will keep your scope local:
$request['items']->each(function($item, $key) use ($order) {
if ($item->delete) {
Item::where('order_id',$order->id)
->where('id',$item['id'])
->delete();
} else {
if (!empty($item['id'])) {
$updateItem = Item::find($item['id']);
$updateItem->number = $item['id'];
$updateItem->save();
}
}
});

Related

PHP foreach loops and data retrieval

Using PHP and MySQL I have generated two arrays. I would like to loop through these arrays, retrieve data from both and display together in one sentence.
foreach ($items as $item) {
if(isset($item->item_title)) {
$itemTitle = $item->item_title;
}
// var_dump($itemTitle);
// string(7) "Halfway" string(5) "Story" string(6) "Listen"
}
foreach ($aData["Items"]["Item"] as $a) {
if (isset($a['description'])) {
$aDescription = $a['description'];
}
// var_dump($aDescription );
// string(4) "Good" string(6) "Strong" string(2) "OK"
}
?>
Desired result;
The title is Halfway and the description is Good.
The title is Story and the description is Strong.
The title is Listen and the description is OK.
// etc
// etc
Is it possible to nest the foreach loops, or is there a better more efficient way?
Please try this way. Hope this help!!
foreach ($items as $index => $item) {
if(isset($item->item_title)) {
$itemTitle = $item->item_title;
echo 'The title is '.$itemTitle;
}
if(isset($aData["Items"]["Item"][$index]['description']) {
$itemDescription = $aData["Items"]["Item"][$index]['description'];
echo ' and the description is '.$itemDescription;
}
echo '<br>';
// The title is Halfway and the description is Good.
}
You can merge those two foreach loops using a simple for loop, like this:
$count = count($items) >= count($aData["Items"]["Item"]) ? count($aData["Items"]["Item"]) : count($items);
for($i = 0; $i < $count; ++$i){
if(isset($item[$i]->item_title)) {
$itemTitle = $item[$i]->item_title;
}
if (isset($aData["Items"]["Item"][$i]['description'])) {
$aDescription = $aData["Items"]["Item"][$i]['description'];
}
// your code
}
Sidenote: The above code assumes that two arrays $items and $aData["Items"]["Item"] have unequal number of elements, though this will work for equal number of elements as well. If you're sure that these two arrays will always have equal number of elements, then refactor the $count = ... ; statement in the following way,
$count = count($items);
or
$count = count($aData["Items"]["Item"]);
and use this $count variable in for loop.
Try this hope this will help you out.
Note: Here i am assuming both array's have same indexes.
$items
$aData["Items"]["Item"].
If not you can do array_values($items) and array_values($aData["Items"]["Item"])
foreach ($items as $key => $item)
{
if (isset($item->item_title) && isset($aData["Items"]["Item"][$key]['description']))
{
$itemTitle = $item->item_title;
echo sprinf("The title is %s and the description is %s",$itemTitle,$aData["Items"]["Item"][$key]['description']);
echo PHP_EOL;
}
}

Change value within array based on input in php

I am trying to locale the correct sub-array in order to change the count, if a specific value is present more than once.
I have the following code:
$trending = [];
foreach($hashtags as $hashtag) {
if(in_array($hashtag->hashtag, $hashtags))
{
array_search()
}
else {
array_push($trending, [
'hashtag' => $hashtag->hashtag,
'counts' => '1'
]);
}
}
This gives me the following example outout:
array(3) {
[0]=> array(2)
{
["hashtag"]=> "foobar"
["counts"]=> "1"
}
[1]=> array(2)
{
["hashtag"]=> "hashtags"
["counts"]=> "1"
}
[2]=> array(2)
{
["hashtag"]=> "imageattached"
["counts"]=> "1"
}
}
So in the foreach loop and the if statement, i want to check for dublicates of hashtags, e.g. if the hashtag foobar exists more than one time, I don't want to create another dublicate in the array, but I want to change the count to 2
How do I find the correct "sub"-array, and change the count of this to 2, if a hashtag is present within $hashtags more than once??
The idea is, that I at the end can sort these arrays, and get the hashtag that is most common, by looking at the count.
If you change the structure of your output, you could do something like this:
$trending = [];
foreach($hashtags as $tag) {
if (isset($trending[$tag])) $trending[$tag]++;
else $trending[$tag] = 1;
}
Which would result in $trending having the structure
array(2) {
["foobar"] => 1,
["hashtags"] => 2
}
Which could then be looped through with
foreach($trending as $tag => $count) {
echo $tag . ' appears ' . $count . ' times.' . PHP_EOL;
}
The PHP method array_count_values might be of some help.
http://php.net/manual/en/function.array-count-values.php
Have you considered using a keyed array?
$trending = array();
foreach($hashtags as $hashtag) {
if(!isset($trending[$hashtag])){
$trending[$hashtag] = 1;
}else{
$trending[$hashtag] += 1;
}
}
By using a keyed array, there is no duplication and you can easily check how frequently a hashtag is used by just accessing $trending[$hashtag]. Additionally, you can get the list of all hashtags in the trending array using $allHashtags = array_keys($trending);.
Of course, if your project specifications do not allow for this, then by all means, use a different approach, but that would be the approach I would take.
It can be more linear of you can change your array structure but for the current this should work.
$trending = [];
$checker = true;
foreach($hashtags as $hashtag) {
foreach ($trending as $key =>$value) {
if($value["hashtag"] == $hashtag->hashtag){
$trending[$key]["counts"]++;
$checker = false;
}
}
if($checker) {
array_push($trending, [
'hashtag' => $hashtag->hashtag,
'counts' => '1'
]);
}
$checker = true;
}

PHP - fetch value of specified key pair in mulit-dimensional array

hopefully a easy one for you,
my sql query is returning a mulit-dimensional array, I need to access only one key the is nested on the second level but cant figure out how.
here is my function.
public function get_visitor_id($id)
{
$this->db->where('mobile',$id);
$this->db->or_where('email',$id);
$this->db->select('uid');
$result = $this->db->get('visitors');
if ($result)
{
foreach ($result->result() as $key=>$value){
$array[$key] = $value;
}
var_dump($array);
return $array;
}
}
The array returned is
{ [0]=> object(stdClass)#20 (1) { ["uid"]=> string(2) "24" } }
I only need the value of ['uid'] so in essence if I was to echo get_visitor_id() it would evaluate to "24".
Thanks for you help.
Cheers
try changing foreach() func to:
foreach($result as $res){
$res = $res->fetch_assoc();
$array['uid'] = $res['uid'];}
EDIT: in case of this didn't work then try while loop:
while($res = $result->fetch_assoc()){
$array['uid'] = $res['uid'];
}

array_search() in Session Array

I tried to use the function array_search but can't get it work..
I got a php session with an array.
array(2) {
[0]=>
array(6) {
["ProductId"]=>string(2) "34"
["ProductName"]=>string(9) "Best ever"
["ProductPrice"]=>string(6) "453.00"
["ProductColor"]=>string(4) "Blue"
["ProductSize"]=>string(1) "S"
["Image"]=>string(36) "d12f95895c9130da8e52a7ff5b9216c9.png"
}
[1]=>
array(6) {
["ProductId"]=>string(2) "33"
["ProductName"]=>string(5) "Vespa"
["ProductPrice"]=>string(7) "1789.00"
["ProductColor"]=>string(4) "Blue"
["ProductSize"]=>string(1) "S"
["Image"]=>string(36) "678e25ea94a7fa94bc6fa427ff29bc6c.png"
}
now I do an array_search()
session_start();
include '_sqlclean.php';
(isset($_POST['product_id'])) ? $p_id = clean_string_save($_POST['product_id']) : $p_id = 0;
$array = $_SESSION['wishList'];
$key = array_search($p_id, $array);
if I do a
var_dump($_SESSION['wishList']);
I got what I showed you above.
But I always got the message "Key not found"
Why ?? What's my mistake ?
I tried already to do
$p_id = "34" // for try
$p_id = intval(34); // for try also
$key = array_search("34", $_SESSION['wishList']); // to see if it works
but nothing worked.. :(
Thanks in advance
array_search will not work for multidimensional arrays. Rather this might work -
$key = array_search($p_id, array_column($array, 'ProductId'));
This will extract all the ProductId from that array then do the search.
Try with alternative for array_search().For example:
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['uid'] === $id) {
return $key;
}
}
return null;
}
OR
$key = array_search($p_id, array_column($array, 'ProductId'));
Aparently you are mistyping the variable (key) name
in the array it is
ProductId
and in your code it is
Product_Id
It happen cause you search into outer array, which contains only [0] and [1] keys.
Try to use
$key = array_search("34", $_SESSION['wishList'][1]);
You can try an alternative way like
<?php
$p_id = "34" // for try
$p_id = intval(34); // for try also
if(in_array($p_id, array_values($_SESSION['wishList']))) {
// Product Id found in your wishList
}
?>
It will works only for single dimension array. For multidimensional use foreach loop.

Duplicates in Associative array

My foreach is entering an array in the second level of my $bad_email array, something like this
["vlley#.rsr.com"] { ["name"]=> "Woo Hilley", ["amount"]=> "125.16"}
["vey#.rsr.com"] { ["name"]=> "Shoo Moo", ["amount"]=> "12.16"}
If I try to enter the same value like ["vlley#.rsr.com"] { ["name"]=> "Woo Hilley", ["amount"]=> "125.16"} again I want it to run specific code. Im not sure how to fix this. The code Im running seems to work when the name is the same but I want the email, name and amount to all match before it fires. Help please
$bad_email = array();
$i = 0;
foreach ($id_array as $key => $id) {
$bad_email[$email][name] =$name;
$bad_email[$email][amount] = $amount;
if ($bad_email[$email][$amount], $bad_email[$email])) {
// DO CODE HERE!!!!
$i++;
}
}
$email, $name, and $amount are all pulled from an api call
This worked...
$bad_email = array();
$temp_email = array();
foreach ($id_array as $key => $id) {
$temp_email =$bad_email;
$bad_email[$email][name] =$name;
$bad_email[$email][amount] = $amount;
if ($bad_email[$email][amount] == $temp_email[$email][amount]){
// DO CODE HERE!!!!
}
}
}

Categories