Solved

Adjust PHP Array

Posted on 2014-04-15
14
320 Views
Last Modified: 2014-04-16
Okay so i have this $array output

Array
(
    [0] => Array
        (
            [0] => 8820
        )

    [1] => Array
        (
            [0] => 102
        )

    [2] => Array
        (
            [0] => 100971
        )

    [3] => Array
        (
            [0] => 5723
        )

    [4] => Array
        (
            [0] => 608898
        )

    [5] => Array
        (
            [0] => 39294
        )

    [6] => Array
        (
            [0] => 4743
        )



how do i adjust this so its new output would be:

Array
        (
            [0] => 8820
            [1] => 102
            [2] => 100971
            [3] => 5723
           ..... and so on

        )
0
Comment
Question by:jporter80
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40003165
Can you post your code, please?
0
 

Author Comment

by:jporter80
ID: 40003168
while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }

print_r($u);
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40003175
while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }
$1dArray=array();
$i = 0;
foreach ($u as $key => $value) {
    $1dArray[$i] = $value;
    $i++;
}

print_r($1dArray);
0
 

Author Comment

by:jporter80
ID: 40003184
that gives me the same thing:

Array
(
    [0] => Array
        (
            [0] => 8820
        )

    [1] => Array
        (
            [0] => 102
        )

    [2] => Array
        (
            [0] => 100971
        )

    [3] => Array
        (
            [0] => 5723
        )

    [4] => Array
        (
            [0] => 608898
        )

    [5] => Array
        (
            [0] => 39294
        )

    [6] => Array
        (
            [0] => 4743
        )

    [7] => Array
        (
            [0] => 439
        )

    [8] => Array
        (
            [0] => 500965
        )
0
 

Author Comment

by:jporter80
ID: 40003190
got it...

while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }
$dArray=array();
foreach ($u as $key => $value) {
    $dArray[$i] = $value[0];
}

print_r($dArray);

not sure if that was the best way.. but it works.
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40003364
No need to loop through the data twice - just use array_shift on the get_post_custom_values to get the first element in the array to add to your $u array.
while ( $q->have_posts() ) { $q->the_post();
    $u[] = array_shift(get_post_custom_values('_u'));
}

print_r($u); 

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40003643
Absent a very large number of posts, repeated iteration won't matter very much.  This is one of those things that is hard to find after optimizing, so there is no reason to optimize the code.  But that said, array_shift() is almost certainly the right tool, if you need a tool.  It would be interesting to see if get_post_custom_values('_u') could be addressed with something like get_post_custom_values('_u')[0] which seems to be the data element we want - the function returns an array and we only want the zero-element.

This is almost certainly wrong, since it injects a time-bomb into the code:
foreach ($u as $key => $value) {
    $dArray[$i] = $value[0];
}

Open in new window

The time-bomb is the variable $i.  Where is this set?  Where is it incremented?  Without knowing that, you can't really know what is going to show up in $dArray, and if another piece of code mutates $i you have the potential for a runtime error -- a real mess.  If you omit $i from $dArray[$i] = $value[0]; and instead write $dArray[] = $value[0]; you remove this potential for error.

PHP arrays are among the most misunderstood data structures.  It's worth reading the online man page for understanding.
http://php.net/manual/en/book.array.php
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/language.operators.array.php
http://php.net/manual/en/function.array.php
http://php.net/manual/en/ref.array.php
http://php.net/manual/en/array.constants.php
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40003850
Absent a very large number of posts, repeated iteration won't matter very much
It won't but this leads to lazy programming which in turn leads to bloat an inefficiency.

Irrespective of the marginal gain - there is always merit in writing the most optimal code. In thise case the optimal solution is also the simplest as it
a) Eliminates several lines of unnecessary code
b) Performs what we would intuitively expect looking at the data.

I would be very wary of following advice that suggests one should not give consideration to writing optimal code.

The $i in the context of the last post is not really relevant here

get_post_custom_values('_u')[0] will only work in 5.4+
< 5.3 requires either array_shift or an intemediary variable. As the version was unknown here I went with what was most backwardly compatible.
0
 

Author Comment

by:jporter80
ID: 40003872
Thanks for the info guys!! It was actually a bad copy and paste.  I dont have the $i in the code im using.

Corrected

while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }
$dArray=array();
foreach ($u as $key => $value) {
    $dArray[] = $value[0];
}

Open in new window


the array will contain probably about 20-50 keys at one time.  Would arrayshift() be best in this situation?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40003882
@julianH: I generally agree with this statement:
always merit in writing the most optimal code
however that assumes that one knows how to write the most optimal code the first time.  If you take ten minutes to make a test for the most optimal code and your optimization gains you a couple of microseconds, you will have to run the code three hundred million times before you recover the ten minutes you spent on the problem.
http://xkcd.com/1205/
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40003903
For better or worse, computer programming is a process that requires exact and meticulous attention to detail.  Errant copy/paste might confuse us (no big deal), and it might also make your scripts fail.  Better to cultivate habits that lead to accuracy.
Would arrayshift() be best...
The function is array_shift() and as julianH points out, you could either choose that or direct [0] notation, depending on the version of PHP.   You can find the version of PHP by running this function: phpinfo().  If you have a sense of humor, set up a test to compare these different programming methods and see which is faster :-)
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40003921
This would be the best way forward in my opinion

while ( $q->have_posts() ) { $q->the_post();
    $u[] = array_shift(get_post_custom_values('_u'));
}

print_r($u); 

Open in new window

So yes array_shift is the best answer here. In addition to being backwardly compatible in my view the [0] is not as clear as array_shift in what you are doing. They perform essentially the same thing so I would go with the array_shift.

As for optomising code.

There is a big difference between

while( ... ) {
   ....
}
foreach() {
  ...
}

Open in new window

and just
while( ... ) {
 ...
}

Open in new window

I can't think of any scenario where there is a valid argument for the former.
0
 

Author Closing Comment

by:jporter80
ID: 40003952
thanks!  works well.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40004000
You are welcome - thanks for the points.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now