Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

array splitting in php

Options
  • 11-10-2009 5:58pm
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    i have an array in php can can be of any length but has varied data types in it (in the conventional sense, i know php isn't a typed language).

    an example of an array could be this.

    $array = array(4, "hello", "joe", 3000000, "foobar");

    now what i want to do is split the array into two arrays,

    1. to store the numeric data, in the above example 4 and 3000000.
    2. to store the string data, in the above example "hello", "joe", "foobar".

    now i've gotten the numeric data out using array_filter().

    e.g.
    $numeric_data = array_filter( $array, "is_numeric" );

    array_filter() won't work the same for strings as "5" and "300000" are considered strings.

    e.g.
    $string_data = array_filter( $array, "is_string" );

    will just return the array untouched.

    anyone have any ideas about how to do this?


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey




  • Registered Users Posts: 9,192 ✭✭✭RobertFoster


    How about is_numeric? The example shown looks like what you're trying to do, except instead of echoing the results, you could populate your new arrays.

    [php]<?php
    $tests = Array(
    "42",
    1337,
    "1e4",
    "not numeric",
    Array(),
    9.1
    );

    foreach($tests as $element)
    {
    if(is_numeric($element))
    {
    echo "'{$element}' is numeric", PHP_EOL;
    }
    else
    {
    echo "'{$element}' is NOT numeric", PHP_EOL;
    }
    }
    ?>[/php]


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    array_filter() won't work the same for strings as "5" and "300000" are considered strings.

    e.g.
    $string_data = array_filter( $array, "is_string" );

    will just return the array untouched.
    Maybe you could write your own callback function instead of using is_string(). It could simply call is_numeric and return TRUE if it is NOT numeric.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    cool that worked.

    now im onto my next problem.

    i have an array of keywords eg. $keywords = array("boo", "joe", "bar", "neck");


    the array to be tested is $text = ("hello", "joe", "bar");

    i will be left with just one element i.e. "hello"



    what i want to do is delete any array elements that contain any of the keywords.

    here's an attempt.

    [php]
    for( $i = 0 ; $i < count($keywords) ; $i++ )
    {
    for( $j = 0 ; $j < count($text) ; $j++ )
    {
    $txt_element = $text[$j];

    echo "keyword is " . $keywords[$i] . " and txt element is : $txt_element <br/>";


    if($keyword[$i] != $text[$j])
    {
    print "boo";
    }
    }
    }
    [/php]

    the word "boo" gets printed everytime even if the elements match.


    sorry in advance for this, it's probably staring me in the face but i haven't programmed in 6 months and it's 14 months since i done any sort of php.


  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    Firstly, why aren't you using foreach?

    Secondly, you should be getting 10 "boo"s printed with that code. Is the problem that 12 are printing, i.e. the if condition is true every time?

    I changed it to this:

    [php]
    $keywords = array("boo", "joe", "bar", "neck");
    $text = array("hello", "joe", "bar")

    foreach ($keywords as $keyword)
    {
    foreach($text as $txt_element)
    {
    if($keyword!= $txt_element)
    {
    print $keyword."!=".$txt_element."\n";
    }
    }
    }[/php]

    When I run this I get:

    boo!=hello
    boo!=joe
    boo!=bar
    joe!=hello
    joe!=bar
    bar!=hello
    bar!=joe
    neck!=hello
    neck!=joe
    neck!=bar

    So it appears to be working...


  • Advertisement
  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    this might help, returns an array of anything not found in keywords

    [PHP]
    $keywords = array("boo", "joe", "bar", "neck");
    $text = array("hello", "joe", "bar");
    $new_array = array();
    foreach($text as $value){
    !(in_array($value,$keywords)) AND $new_array[]=$value;
    }
    print_r($new_array);
    /* results in

    Array ( [0] => hello )

    */
    [/PHP]


  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    :S

    Why on earth would you use short circuit evaluation as opposed to an if there?


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    :S

    Why on earth would you use short circuit evaluation as opposed to an if there?
    just a habit of mine, the word AND makes it very easy to read code IMO. Would an if be better?


  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    I just don't feel that it makes what's happening very specific, I mean anyone reading that who didn't know about short circuit evaluation, or had only seen it used in branch conditions would have a hard time deciphering that. Now I don't really use PHP, so perhaps it's an idiom I'm unaware of (similar to "or die" in Perl), but I don't think so, and unless that's the case I would encourage avoiding it. An if is much more readable IMO.


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    I just don't feel that it makes what's happening very specific, I mean anyone reading that who didn't know about short circuit evaluation, or had only seen it used in branch conditions would have a hard time deciphering that. Now I don't really use PHP, so perhaps it's an idiom I'm unaware of (similar to "or die" in Perl), but I don't think so, and unless that's the case I would encourage avoiding it. An if is much more readable IMO.
    I disagree it is hard to decipher. I started using it a while back when I started working with the Kohana framework. The developers use it quite a bit and it kinda stuck with me.

    I use it when I'm simply altering one variable. I use an if block for nearly everything else. For example in the constructor for a class I would have a few lines of this type of operator to populate the class variables.

    (boolean) AND -> I know a variable is changing

    if (boolean) -> I know x,y or z is happening

    Once you get used to this style of coding it helps figure out what is going on in a class a lot faster (IMO!)


  • Advertisement
  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    hey guys thanks that worked a treat.

    i've now got another problem that's related to the same php page, but further on down the list.


    [php]
    function dorequest()
    {
    $response = $DBobj->makerequest($parameters);
    $results = $response->results;

    foreach($results->adverts as $ad)
    {
    printf('<a href="%s">%s</a><br />', $ad->url, $ad->text);
    }
    }
    [/php]

    that will give me a list of ad links like so
    <a href="http://localhost/blah/12345">bike, Bray, Co. Wicklow</a>
    <a href="http://localhost/blah/12346">car, Bray, Co. Wicklow</a>
    ...
    ...
    <a href="http://localhost/blah/12343445">car, Shankill, Co. Dublin</a>
    

    what i want to be able to do is filter these results based on the town that was submitted.

    but if a town is not submitted i want to display all results.

    what would best approach for this. this programming lark ain't easier after a good few months off :D


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Surely you should do the filtering in the SQL select statement instead of loading all into memory and then wasting time filtering.

    Then again, I'm not sure how your makerequest method works.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    Webmonkey wrote: »
    Surely you should do the filtering in the SQL select statement instead of loading all into memory and then wasting time filtering.

    Then again, I'm not sure how your makerequest method works.
    yeh lets just say i have no control over the SQL, i purely pass it parameters and out pops the data.

    just for arguments sake the code above isn't actually the code but an example of the problem i'm trying to overcome.

    if i had my way i would use SQL to filter instead of using php.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Well that sucks :)

    Anyways, if you have a town, you should just iterate through the array and pop out anything that isn't that town using regular expressions or what ever and return the remainder.

    [php]
    $filteredList = filter_town($array, 'town');
    [/php]


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    Thanks donal, all sorted now :)

    *EDIT* how do i change the tag to say it's solved now? i tried editing the original post but no luck.


Advertisement