Aziz Saleh – Software development

  • Home
  • Downloads
    • AzizMVC Downloads
      • Database Plugin
        • Template Plugin
    • Flash Builder 4
      • Bypass, Hijack, Beat, and Defeat Same Origin Policy
      • Flash Click Tracker
      • Flash Link Hider
      • Pacman source V 1.0
    • Java Downloads
      • Currency Converter
    • Javascript Downloads
    • PHP/MySQL Downloads
      • Aziz MVC
      • AzizMVC Documentation
      • Contact Form Maker
      • NetFlix API 1.0 Wrapper
      • Simple Blog
    • Visual Basic Downloads
      • Currency Converter
        • Key Logger w/Store, email, registry options
  • Resume & Portfolio
  • Ask me a question
  • Contact Me

Posted on December 1, 2010 by Aziz Posted in PHP

These are two functions to manipulate PHP objects.

The first function is similar to array_merge function in that it merges two objects based on an index.

The second function in_object also acts like in_array to which it checks if a certain index exists in an array at a specific value.

Hopefully someone finds it helpful.

 

<?php

// Object merge and in Object functions
function object_merge($objectFrom,$objectTo,$indexUse) 
{ 
    $objectReturn = array(); 
    foreach($objectFrom as $curObject) 
    {         
        $index = in_object($indexUse,$curObject->$indexUse,$objectTo,true); 
        if(is_int($index)) 
        { 
            foreach($objectTo[$index] as $indexName => $indexValue) 
            { 
                $curObject->$indexName = $indexValue; 
            } 
        } 
        $objectReturn[] = (object) $curObject;         
    } 
    return $objectReturn; 
} 

function in_object($searchFor,$searchValue,$objectSearch,$returnResult) 
{ 
    foreach($objectSearch as $index => $thisObject) 
    { 
        if($thisObject->$searchFor == $searchValue) 
        { 
            if($returnResult) 
            { 
                return $index; 
            } else { 
                return true; 
            } 
        } 
    } 
    return false; 
} 

// Usage 
$obj1[0] = (object) array('catId' => 1, 'catName' => 'Name'); 
$obj1[1] = (object) array('catId' => 2, 'catName' => 'Name2'); 

$obj2[0] = (object) array('catId' => 1, 'catDesc' => 'Desc'); 
$obj2[1] = (object) array('catId' => 2, 'catDesc' => 'Desc2'); 

$obj_merged = object_merge($obj1,$obj2,'catId'); 
print_r($obj_merged);
« Flash Link Hider
Key Logger with Save, Email and Registry Save for Auto Load »

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • PHP does not have MySQL support enabled.
  • Your PHP installation appears to be missing the MySQL extension which is required by WordPress
  • Undefined mysql functions (mysql_connect, mysql_select_db, mysql_query) – PDO alternative
  • Checking if you have Javascript installed by PHP
  • Process Maxmind countries, regions and cities – Parser to MySQL or SqLite

Recent Comments

  • fathima on Currency Converter Using Java Netbeans
  • Chris on Currency Converter
  • Khalid Ahmad Qweder on Resume & Portfolio
  • Michel on Currency Converter
  • Michel on Currency Converter

Archives

  • March 2014
  • September 2013
  • November 2012
  • October 2012
  • November 2011
  • February 2011
  • December 2010
  • November 2010
  • August 2010
  • May 2010
  • September 2009

Categories

  • Flash Builder 4
  • General
  • Java
  • Javascript
  • MySQL
  • PHP
  • Visual Basic

Meta

  • Register
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
CyberChimps ©2023