Avoid duplicate element in foreach - php

I would like the code below to display only one time each $row[2] element (no duplicates) :
foreach($rows as $row){
echo " {$row[2]} ";
}
How can I achieve this ? Thanks.
My array is very big but here is a sample from var_dump
[0]=>
array(10) {
[0]=> string(2) "39"
["id"]=> string(2) "39"
[1]=> string(3) "abc"
["A"]=> string(3) "abc"
[2]=> string(2) "123"
["B"]=> string(2) "123"
[3]=> string(1) "0"
["C"]=> string(1) "0"
[4]=> string(1) "1"
["D"]=> string(1) "1"
}
I'm only interested about [2]=> string(2) "123".

Here is the code you can use:
$uniqueArr = array();
foreach ($rows as $row) {
if(!(in_array($row[2], $uniqueArr))) {
echo $row[2];
$uniqueArr[] = $row[2];
}
}

Related

How to create new arrays from object array based on user_id?

I'm having trouble looping through an object array and displaying data from it under certain conditions.
I want the webpage to display the following:
<p>John Doe</p> <p>William Green</p> <p>Jane Smith</p>
I know how to do this by defining $user[0]->first_name . $user[0]->last_name specifically, but what I need is a way for the code to loop through and display the names dynamically using the user_id property as a unique identifier.
For all objects in the array with user_id 1, return "these values." For all the objects in the array with user_id 2, return "these values." And so on...looping through each unique user_id.
Here is a var_dump of the object array:
array(4)
{ [0]=> object(stdClass)#4336 (4)
{
["umeta_id"]=> string(1) "1"
["user_id"]=> string(1) "1"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(4) "John"
}
[1]=> object(stdClass)#4333 (4)
{
["umeta_id"]=> string(1) "2"
["user_id"]=> string(1) "1"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(3) "Doe"
}
[2]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "3"
["user_id"]=> string(1) "2"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(4) "Jane"
}
[3]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "4"
["user_id"]=> string(1) "2"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(5) "Smith"
}
[4]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "5"
["user_id"]=> string(1) "3"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(7) "William"
}
[5]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "6"
["user_id"]=> string(1) "3"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(5) "Green"
}
}
When I output the information on the webpage, I want it to look like this:
<p>John Doe</p> <p>William Green</p> <p>Jane Smith</p>
Maybe I need to loop through objects and build new arrays based on user_ids? Please do not provide code with echo or print_r. Only "return" can be used for this project.
So $your_object is already sorted by umeta_id and user_id fields?
If not, you can use usort:
function cmp1($a, $b)
{
return strcmp($a->umeta_id, $b->umeta_id);
}
function cmp2($a, $b)
{
return strcmp($a->user_id, $b->user_id);
}
usort($your_object, "cmp1");
usort($your_object, "cmp2");
Then simply:
$str = '';
for($i=0;$i<count($your_object);$i+=2){
$str .= "<p>".$your_object[$i]->meta_value." "$your_object[$i+1]->meta_value."</p> ";
}
return $str; // this var contain all your data in string
Hope this helps!
This will generate the first-name and last-name pairs :
$user_group = array();
ksort($user_group, SORT_NUMERIC);
foreach ($users as $key => $item) {
$name_meta = get_object_vars($item);
foreach ($name_meta as $meta_in => $names) {
if ($names == 'first_name') {
$user_group[$item->user_id]['first_name'] = $name_meta['meta_value'];
}
if ($names == 'last_name') {
$user_group[$item->user_id]['last_name'] = $name_meta['meta_value'];
}
}
}
Output :
array(3) {
[1]=>
array(2) {
["first_name"]=>
string(4) "John"
["last_name"]=>
string(3) "Doe"
}
[2]=>
array(2) {
["first_name"]=>
string(4) "Jane"
["last_name"]=>
string(5) "Smith"
}
[3]=>
array(2) {
["first_name"]=>
string(7) "William"
["last_name"]=>
string(5) "Green"
}
}
And to print the user name pairs :
foreach ($user_group as $name) {
echo "<p>" . $name['first_name'] . " " . $name['last_name'] . "</p>";
}

PHP push new data into given array

I may seem having difficulty in understanding on how array works. So result, I can't work on the issue.
foreach($results as $key => $value){
$product_key = array(
'key' => $key
);
array_push($results, $product_key);
}
var_dump($results); exit;
Expected Output
array(2) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
["key"]=>
int(0)
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
["key"]=>
int(1)
}
Unexpected Output
array(4) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
}
[2]=>
array(1) {
["key"]=>
int(0)
}
[3]=>
array(1) {
["key"]=>
int(1)
}
}
You push new value (which is array) to the end of existsing array, what do you expect then?
If you want to modify current interated array value use this approach:
foreach($results as $key => $value) {
// use `->` as `$value` is object
$value->key = $key;
}
var_dump($results); exit;

Get specific value from PHP array using foreach key value

I have the following array:
array(15) {
[0]=> object(stdClass)#317 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" }
[1]=> object(stdClass)#316 (2) { ["id"]=> string(1) "3" ["value"]=> string(531) "awfaww" }
[2]=> object(stdClass)#315 (2) { ["id"]=> string(1) "4" ["value"]=> string(1) "1" }
[3]=> object(stdClass)#318 (2) { ["id"]=> string(1) "5" ["value"]=> string(1) "1" }
[4]=> object(stdClass)#319 (2) { ["id"]=> string(1) "6" ["value"]=> string(1) "1" }
[5]=> object(stdClass)#320 (2) { ["id"]=> string(1) "7" ["value"]=> string(1) "1" }
[6]=> object(stdClass)#321 (2) { ["id"]=> string(1) "8" ["value"]=> string(1) "1" }
[7]=> object(stdClass)#322 (2) { ["id"]=> string(2) "30" ["value"]=> string(8) "12:30:02" }
[8]=> object(stdClass)#323 (2) { ["id"]=> string(2) "31" ["value"]=> string(8) "18:12:00" }
[9]=> object(stdClass)#324 (2) { ["id"]=> string(2) "11" ["value"]=> string(10) "2014-06-17" }
[10]=> object(stdClass)#325 (2) { ["id"]=> string(2) "12" ["value"]=> string(10) "2014-06-26" }
[11]=> object(stdClass)#326 (2) { ["id"]=> string(2) "14" ["value"]=> string(1) "2" }
[12]=> object(stdClass)#327 (2) { ["id"]=> string(2) "15" ["value"]=> string(1) "2" }
[13]=> object(stdClass)#328 (2) { ["id"]=> string(2) "16" ["value"]=> string(1) "4" }
[14]=> object(stdClass)#329 (2) { ["id"]=> string(2) "17" ["value"]=> string(1) "5" }
}
I would like to get a specific value from this array using the ID. For example, if the ID: 11 is found in the array I want to retrieve its value. How can I do this?
Try something like this:
<?php
function findById($array, $id) {
foreach ($array as $value) {
if ($value->id == $id) {
return $value->value;
}
}
return null;
}
$result = findById($yourArray, 11);
?>
if the array is static for each run - i'd suggest changing the array into "key"=>"value" array, using this:
$new_arr = array();
foreach($original_array as $object) {
$new_arr[$object->id] = $object->value;
}
and then you can just use $new_arr[id], instead of searching the whole original array each time.
You can use array_filter:
array_filter($arr, function($i) { return $i->id == '11'; });
See Documentation

How to divide array in parts with a token?

I have an array
foreach ($infos->find('.products-list-item .products-list-item__sizes') as $sizes_info)
{
while($size = $sizes_info->children($k++))
{
$sizes_arr[] = $size->plaintext;
}
$sizes_arr[] = '_';
$k=0;
}
it looks like:
array(412) { [0]=> string(2) "XXL" [1]=> string(1) "S" [2]=> string(1) "M" [3]=> string(1) "L" [4]=> string(1) "_" [5]=> string(2) "43" [6]=> string(2) "44".....and so on
I need to divide this array in arrays with by a sign "_"... in other words I need to get this:
array(4) { [0]=> string(2) "XXL" [1]=> string(1) "S" [2]=> string(1) "M" [3]=> string(1) "L"}
array(2) { [0]=> string(2) "43" [1]=> string(1) "44"}
thanks in advance!
Why you're forming your array in such way if you need it in another way? Simply do:
foreach ($infos->find('.products-list-item .products-list-item__sizes') as $sizes_info) {
$sizes_tmp = []; //or $sizes_tmp = array(); for PHP<=5.3
while ($size = $sizes_info->children($k++)) {
$sizes_tmp[] = $size->plaintext;
}
$sizes_arr[] = $sizes_tmp;
$k=0;//not sure what is it. leaving it as it is
}

getting a unique values from array

I currently have an array that looks like the one below but I only want to display show results in the array from which are unique (description), I presume with array_unique? but I keep getting the same results?
Here is my array:
$taglist = array(5) {
[0]=> array(5) {
["id"]=> string(2) "27"
["page_id"]=> string(2) "18"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
[1]=> array(5) {
["id"]=> string(2) "29"
["page_id"]=> string(2) "18"
["description"]=> string(3) "Tutorials"
["slug"]=> string(3) "tutorials"
["visibility"]=> string(7) "visible"
}
[2]=> array(5) {
["id"]=> string(2) "31"
["page_id"]=> string(2) "21"
["description"]=> string(3) "tag"
["slug"]=> string(3) "tag"
["visibility"]=> string(7) "visible"
}
[3]=> array(5) {
["id"]=> string(2) "32"
["page_id"]=> string(2) "21"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
}
Here is my while:
$items = array();
$results = $taglist;
foreach ($results as $result)
{
$items[]= $result['description'];
$items = array_unique($items);
}
echo '<ul>';
while ($tag_item = current($items))
{
echo '<li>'.$tag_item['description'].'</li>';
next($items);
}
echo '</ul>';
$taglist = array(
0 => array('description'=>'one'),
1 => array('description'=>'two'),
2 => array('description'=>'one'),
3 => array('description'=>'three'),
4 => array('description'=>'one'),
);
// echo var_export($taglist, 1); // uncomment to see the diff vs var_dump()
foreach($taglist as $tag){
$new[] = $tag['description'];
}
var_dump(array_unique($new));

Categories